function Transpaydept() {
    this.form = this.InitForm();

    var year = $("#hidyear").val();
    var quarter = $("#hidquarter").val();
    $("#year").val(year);
    $("#quarter").val(quarter);

    var month = 3;//根据季度来设置月份
    if (quarter == 2) month = 6;
    if (quarter == 3) month = 9;
    if (quarter == 4) month = 12;
    $("#hidmonth").val(month);

    this.form.render('select');
    $("#bottom_btns").hide();

    this.orgsuytable = this.InitOrgSuyTable();
    this.tab = this.InitTab();
    this.table = null;

}

Transpaydept.prototype.InitTab = function () {
    var element;
    layui.use('element', function () {
        element = layui.element;

        element.on('tab(tabcontent)', function (data) {
            if (data.index == 1) {
                $("#bottom_btns").show();
                if ($("#hidstatus").val() != '') {
                    var status = Number($("#hidstatus").val());
                    if (status > 0) {
                        $("#btnbmtj").attr("disabled", "disabled");
                        $("#btnbmtj").html("该部门已确认");
                    }
                }
            } else {
                $("#hidorgid").val(0);
                $("#hidorgsummaryid").val(0);
                $("#hidstatus").val('');
                $("#bottom_btns").hide();
                $("#btnbmtj").removeAttr("disabled");

                $("#realname").val("");
            }
        });

    });
    return element;
}

Transpaydept.prototype.OpTab = function (tabindex, obj) {
    this.tab.tabChange('tabcontent', tabindex);
}

Transpaydept.prototype.InitForm = function () {
    var form;
    layui.use('form', function () {
        form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        form.on('select(year)', function (data) {
            $("#hidyear").val(data.value);
            form.render('select');
            vWorksPayDept.searchtableorgsuy();
        });
        form.on('select(quarter)', function (data) {
            $("#hidquarter").val(data.value);
            form.render('select');
            vWorksPayDept.searchtableorgsuy();
        });

        form.render();
    });
    return form;
}

Transpaydept.prototype.searchlist = function () {
    var month = 3;//根据季度来设置月份
    var quarter = $("#hidquarter").val();
    if (quarter == 2) month = 6;
    if (quarter == 3) month = 9;
    if (quarter == 4) month = 12;
    $("#hidmonth").val(month);

    this.grid.setOptions({
        newPage: 1, parms: {
            Method: "",
            year: $("#hidyear").val(),
            month: $("#hidmonth").val()
        }
    });
    this.grid.loadData();
}

Transpaydept.prototype.InitOrgSuyTable = function () {
    var table;
    var _this = this;
    layui.use('table', function () {
        table = layui.table;
        table.render({
            elem: '#orgsuylist'
            , height: $(window).height() - 140
            , url: 'get_org_work_dept_list' // 数据接口
            , page: true // 开启分页
            , limit: 30
            , id: 'orgsuylist'
            , method: "post"
            , cols: [[
                {type: 'numbers'}
                , {field: 'orgname', title: '单位名称', width: 250, align: 'center'}
                , {field: 'year', title: '年', width: 100, align: 'center'}
                , {field: 'quarter', title: '季度', width: 100, align: 'center'}
                // , {field: 'month', title: '月份', width: 100, align: 'center'}
                // , {field: 'orgsummaryid', title: '部门人数', width: 100, align: 'center'}
                , {field: 'status', title: '状态', width: 120, align: 'center'}
                , {title: '操作', width: 100, align: 'left', toolbar: '#operateorgsuy'}
            ]]
            , request: {
                type: 'post'
            }
            , page: false
            , where: {
                year: $("#hidyear").val(),
                month: $("#hidmonth").val()
            }
            , even: true //开启隔行背景
            , done: function (res, page, count) {
                var dataarr = res.data;
                $("[data-field='status']").children().each(function (index) {
                    if (index > 0) {
                        var text = $(this).text();
                        var thisdata = dataarr[index - 1];
                        if (!thisdata.usercount || thisdata.usercount <= 1) {
                            text = '9';
                        }
                        switch (text) {
                            case null:
                                $(this).text("未填报");
                                break;
                            case '':
                                $(this).text("未填报");
                                break;
                            case '0':
                                $(this).text("填报");
                                break;
                            case '1':
                                $(this).text("确认");
                                break;
                            case '2':
                                $(this).text("归档");
                                break;
                            case '3':
                                $(this).text("驳回");
                                break;
                            case '4':
                                $(this).text("审核通过");
                                break;
                            case '9':
                                $(this).text("");
                                break;
                        }
                    }
                })
                $("[data-field='quarter']").children().each(function (index) {
                    if (index > 0) {
                        var text = $(this).text();
                        switch (text) {
                            case null:
                            case '':
                                var month = dataarr[index - 1].month;
                                if (month == 1 || month == 2 || month == 3) {
                                    $(this).text("第一季度");
                                }
                                if (month == 4 || month == 5 || month == 6) {
                                    $(this).text("第二季度");
                                }
                                if (month == 7 || month == 8 || month == 9) {
                                    $(this).text("第三季度");
                                }
                                if (month == 10 || month == 11 || month == 12) {
                                    $(this).text("第四季度");
                                }
                                break;
                            case '1':
                                $(this).text("第一季度");
                                break;
                            case '2':
                                $(this).text("第二季度");
                                break;
                            case '3':
                                $(this).text("第三季度");
                                break;
                            case '4':
                                $(this).text("第四季度");
                                break;
                        }
                    }
                })
                // $("[data-field='orgsummaryid']").children().each(function (index) {
                //     if (index > 0) {
                //         var thisdata = dataarr[index - 1];
                //         var text = $(this).text();
                //         if (text == null || text == '') {
                //             var percount = thisdata.usercount;
                //             $(this).text(percount);
                //         } else {
                //             var realusercount = thisdata.realusercount;
                //             $(this).text(realusercount);
                //         }
                //     }
                // })
            }
        });


        //监听工具条
        table.on('tool(orgsuylist)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                $("#hidorgid").val(data.orgid);
                $("#hidorgsummaryid").val(data.orgsummaryid);
                $("#hidstatus").val(data.status);
                $("#hidmonth").val(data.month);


                var quarter = 1;
                if (data.month == 4 || data.month == 5 || data.month == 6) quarter = 2;
                if (data.month == 7 || data.month == 8 || data.month == 9) quarter = 3;
                if (data.month == 10 || data.month == 11 || data.month == 12) quarter = 4;
                $("#hidquarter").val(quarter);
                _this.OpTab(2, data);
                $("#table_title").html("房山公安分局" + data.orgname + " " + data.year + " 年第" + quarter + " 季度工作性津贴");

                _this.table = _this.InitTableByBt(data.orgid, data.orgsummaryid);
            } else if (obj.event === 'tijiao') {
                vWorksPayDept.checkInputDate(data.year, $("#hidquarter").val(), data.orgid, 1);
            }
        });
    });

    return table;
}

Transpaydept.prototype.querydetail = function () {
    if ($("#hidorgid").val() == "" || $("#hidorgid").val() == "0") {
        top.mylayui.msg('请先点机构列表后面详情按钮，再点击查询', {icon: 0});
        return;
    }

    this.InitTableByBt($("#hidorgid").val(), $("#hidorgsummaryid").val());
}

Transpaydept.prototype.InitTableByBt = function (orgid, orgsummaryid) {
    var quarter = $("#hidquarter").val();
    var month1 = "一月";
    var month2 = "二月";
    var month3 = "三月";
    if (quarter == 2) {
        month1 = "四月";
        month2 = "五月";
        month3 = "六月"
    }
    if (quarter == 3) {
        month1 = "七月";
        month2 = "八月";
        month3 = "九月"
    }
    if (quarter == 4) {
        month1 = "十月";
        month2 = "十一月";
        month3 = "十二月"
    }

    var table;
    var _this = this;
    if (_this.table != null) {
        _this.table.reload('attfjgdlist', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                year: $("#hidyear").val(),
                month: $("#hidmonth").val(),
                quarter: quarter,
                orgid: orgid,
                realname: $("#realname").val()
            }
        });
    }
    else {
        layui.use('table', function () {
            table = layui.table;
            table.init('attfjgdlist', {
                height: $(window).height() - 210
                , url: 'get_work_dept_list' // 数据接口
                , page: true // 开启分页
                , limit: 40
                , id: 'attfjgdlist'
                , limits: [20, 40, 80, 100]
                , cols: [
                    [
                        {title: '扣减情况', align: 'center', colspan: 8}
                        , {title: '增加情况', align: 'center', colspan: 4}
                    ],
                    [ // 表头
                        {field: 'realname', title: '姓名', width: 80, align: 'center'}
                        , {field: 'orgname', title: '部门', width: 120, align: 'center'}
                        , {field: 'cardno', title: '身份证号', width: 120, align: 'center'}
                        , {field: 'downreason', title: '原因', width: 120, align: 'center'}
                        , {field: 'workpernum1', title: '' + month1 + '', width: 80, align: 'center'}
                        , {field: 'workpernum2', title: '' + month2 + '', width: 80, align: 'center'}
                        , {field: 'workpernum3', title: '' + month3 + '', width: 80, align: 'center'}
                        , {field: 'workpernumsum', title: '合计', width: 180, align: 'center'}
                        , {field: 'realname', title: '姓名', width: 80, align: 'center'}
                        , {field: 'cardno', title: '身份证号', width: 120, align: 'center'}
                        , {field: 'upreason', title: '原因', width: 120, align: 'center', edit: 'text'}
                        , {field: 'upmoney', title: '金额', width: 120, align: 'center', edit: 'text'}
                        , {field: 'status', title: '状态', width: 120, align: 'center'}
                    ]
                ]
                , method: "post"
                , request: {
                    type: 'post'
                }
                , where: {
                    year: $("#hidyear").val(),
                    month: $("#hidmonth").val(),
                    quarter: quarter,
                    orgid: orgid,
                    realname: $("#realname").val()
                }
                , done: function (res, page, count) {


                    var dataarr = res.data;

                    $("[data-field='upreason']").children().each(function (index) {

                        $($(this)[0]).addClass('cellColor');


                    })

                    $("[data-field='upmoney']").children().each(function (index) {
                        $($(this)[0]).addClass('cellColor');
                    })

                    $("[data-field='upreason']").children().each(function (index) {
                        $($(this)[0]).addClass('cellColor');
                    })

                    $("[data-field='status']").children().each(function (index) {
                        if (index > 0) {
                            var text = $(this).text();
                            switch (text) {
                                case null:
                                    $(this).text("未填报");
                                    break;
                                case '':
                                    $(this).text("未填报");
                                    break;
                                case '0':
                                    $(this).text("填报");
                                    break;
                                case '1':
                                    $(this).text("确认");
                                    break;
                                case '2':
                                    $(this).text("归档");
                                    break;
                                case '3':
                                    $(this).text("驳回");
                                    break;
                                case '4':
                                    $(this).text("审核通过");
                                    break;
                            }
                        }
                    })


                    $("[data-field='sfqq']").children().each(function (index) {
                        if (index > 0) {
                            var text = $(this).text();
                            if (text == "0") {
                                $(this).text("否");
                            }
                            else {
                                $(this).text("是");
                            }
                        }
                    })

                }


            });
            // table.on('edit(attfjgdlist)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            //
            //         if (obj.field == "upmoney") {
            //
            //             var downmoney = parseInt(obj.data.workpernumsum);
            //             var upmoney = parseInt(obj.value);
            //
            //             var summoney = upmoney + downmoney;
            //             console.log(summoney);
            //             if (summoney != 0) {
            //                 layer.msg('金额不一致', {icon: 0})
            //
            //                 if (!hs.containKey(obj.data.LAY_TABLE_INDEX)) {
            //
            //                     hs.add(obj.data.LAY_TABLE_INDEX, 1);
            //                 }
            //             } else {
            //                 if (hs.containKey(obj.data.LAY_TABLE_INDEX)) {
            //
            //                     hs.remove(obj.data.LAY_TABLE_INDEX);
            //                 }
            //             }
            //
            //         }
            //     });
        })
    }
    return table;
}

Transpaydept.prototype.addresult = function (result, msg) {
    if (result > 0) {
        top.mylayui.msg('操作成功', {icon: 1});
        top.mylayui.closeAll('iframe');
        vWorksPayDept.searchtable();
    }
    else {
        if (msg) {
            top.mylayui.msg(msg, {icon: 0});
        } else {
            top.mylayui.msg('操作失败', {icon: 0});
        }
    }
}

Transpaydept.prototype.searchtable = function () {
    this.table.reload('attfjgdlist', {
        page: {
            curr: 1 //重新从第 1 页开始
        }
        , where: {
            year: $("#year").val(),
            month: $("#month").val()
        }
    });
}


Transpaydept.prototype.exportexcel = function () {
    if ($("#hidorgid").val() == "" || $("#hidorgid").val() == "0") {
        top.mylayui.msg('请先点机构列表后面详情按钮，再点击查询', {icon: 0});
        return;
    }
    top.ShowZheZhao();
    $.ajax({
        type: "POST",
        url: "export_excel",
        data: {
            year: $("#year").val(),
            month: $("#month").val(),
            orgid: $("#hidorgid").val(),
            orgsummaryid: $("#hidorgsummaryid").val()
        },
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                //top.mylayui.msg(data.msg, {icon: 1});
                //alert(data.msg);
                window.open(data.msg, '_self');
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
            top.ShowZheZhao();

        },
        complete: function (XHR) {
            top.mylayui.closeAll('loading');
        },
        error: function () {

        }
    });
}


Transpaydept.prototype.searchtableorgsuy = function () {
    var month = 3;//根据季度来设置月份
    var quarter = $("#hidquarter").val();
    if (quarter == 2) month = 6;
    if (quarter == 3) month = 9;
    if (quarter == 4) month = 12;
    $("#hidmonth").val(month);

    this.orgsuytable.reload('orgsuylist', {
        page: {
            curr: 1 //重新从第 1 页开始
        }
        , page: false
        , where: {
            year: $("#hidyear").val(),
            month: $("#hidmonth").val()
        }
    });
}

Transpaydept.prototype.btntijiao = function () {
    // var orgsummaryid=$("#hidorgsummaryid").val();
    var year = $("#year").val();
    var quarter = $("#hidquarter").val();
    var orgid = $("#hidorgid").val();
    // vWorksPayDept.updatestatus(year, quarter, orgid, 1);
    vWorksPayDept.checkInputDate(year, quarter, orgid, 1);
    //刷新
    vWorksPayDept.querydetail();
}


Transpaydept.prototype.updatestatus = function (year, quarter, orgid, status) {
    //改到提交时验证数据的正确性\update_work_pay_check



    top.mylayui.confirm('点击确认后，津贴会提交到部门领导进行审批', {icon: 0}, function (index, dd) {
        top.mylayui.close(index);
        $.ajax({
            type: "POST",
            url: "update_work_pay_status",
            data: {year: year, quarter: quarter, orgid: orgid, status: status},
            beforeSend: function (XHR) {

            },
            success: function (data) {
                data = eval('(' + data + ')');
                if (data.result > 0) {
                    top.mylayui.msg(data.msg, {icon: 1});
                    vWorksPayDept.searchtableorgsuy();
                }
                else {
                    top.mylayui.msg(data.msg, {icon: 0});
                }
            },
            complete: function (XHR) {

            },
            error: function () {

            }
        });
    })
}

Transpaydept.prototype.checkInputDate = function(year, quarter, orgid,status){
    $.ajax({
        type: "POST",
        url: "update_work_pay_check",
        data: {year: year, quarter: quarter, orgid: orgid},
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                top.mylayui.msg(data.msg, {icon: 1});
                vWorksPayDept.updatestatus(year, quarter, orgid,status);
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
        },
        complete: function (XHR) {

        },
        error: function () {

        }
    });
}


Transpaydept.prototype.save = function () {
    // if (hs.getSize() > 0) {
    //     layer.msg('存在金额不一致，请修正', {icon: 0})
    //     return;
    // }

    var tabledataJson = this.table.cache;
    delete tabledataJson.orgsuylist;//怪异，多出来的属性
    console.log(tabledataJson);
    var quarter = $("#hidquarter").val();
    var year = $("#year").val();
    tabledataJson['year'] = year;
    tabledataJson['quarter'] = quarter;
    tabledataJson = JSON.stringify(tabledataJson);
    tabledataJson = tabledataJson.replace(/LAY_TABLE_INDEX/g, 'lay_TABLE_INDEX');
    $.ajax({
        type: "POST",
        url: "update_work_pay",
        contentType: 'application/json',
        data: tabledataJson,
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                top.mylayui.msg(data.msg, {icon: 1});
                // vWorksPayDept.querydetail();
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
        },
        complete: function (XHR) {

        },
        error: function () {

        }
    });
}

var hs = new HashTable();
var vWorksPayDept = null;
$(function () {
    vWorksPayDept = new Transpaydept();
})